package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.base.query.QueryObject;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class CarPackageAuditServiceImpl implements ICarPackageAuditService {
    @Resource
    private CarPackageAuditMapper carPackageAuditMapper;
    @Resource
    private IBpmnInfoService bpmnInfoService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private RepositoryService repositoryService;
    @Resource
    private IServiceItemService serviceItemService;
    @Resource
    private TaskService taskService;

    @Override
    public CarPackageAudit get(Long id) {
        return carPackageAuditMapper.selectOne(id);
    }

    @Override
    public TablePageInfo<CarPackageAudit> query(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<>(carPackageAuditMapper.queryForList(qo));
    }

    @Override
    public void save(CarPackageAudit carPackageAudit) {
        carPackageAuditMapper.insert(carPackageAudit);
    }

    @Override
    public void update(CarPackageAudit carPackageAudit) {
        carPackageAuditMapper.update(carPackageAudit);
    }

    @Override
    public void deleteById(Long id) {
        carPackageAuditMapper.deleteById(id);
    }

    @Override
    public InputStream getProcessImageByAuditId(Long id) {
        CarPackageAudit carPackageAudit = this.get(id);
        BpmnInfo bpmnInfo = bpmnInfoService.get(carPackageAudit.getBpmnInfoId());
        List<String> activeActivityIds;
        // 如果处于审核中, 高亮节点
        if (CarPackageAudit.STATUS_IN_ROGRESS.equals(carPackageAudit.getStatus())) {
            activeActivityIds = runtimeService.getActiveActivityIds(carPackageAudit.getInstanceId());
        } else {
            activeActivityIds = new ArrayList<>();
        }
        DefaultProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
        BpmnModel bpmnModel = repositoryService.getBpmnModel(bpmnInfo.getActProcessId());
        return generator.generateDiagram(bpmnModel, activeActivityIds, Collections.EMPTY_LIST, "宋体", "宋体", "宋体");
    }

    @Override
    public void cancelApply(Long id) {
        CarPackageAudit carPackageAudit = this.get(id);
        carPackageAuditMapper.changeAuditStatus(id, CarPackageAudit.STATUS_CANCEL);
        serviceItemService.changeAuditStatus(carPackageAudit.getServiceItemId(), ServiceItem.AUDITSTATUS_INIT);
        runtimeService.deleteProcessInstance(carPackageAudit.getInstanceId(), "撤销审核");
    }

    @Override
    public void audit(Long id, Integer auditStatus, String info) {
        CarPackageAudit carPackageAudit = this.get(id);
        if (!CarPackageAudit.STATUS_IN_ROGRESS.equals(carPackageAudit.getStatus())) {
            throw new BusinessException("审核中的记录才能审核");
        }
        String userName = ShiroUtils.getUser().getUserName();
        if (CarPackageAudit.STATUS_PASS.equals(auditStatus)) {
            info = userName + "【同意】, 批注意见:" + info;
        } else {
            info = userName + "【拒绝】, 批注意见:" + info;
        }
        carPackageAudit.setInfo(carPackageAudit.getInfo() + "<br>" + info);
        Task currentTask = taskService.createTaskQuery().processInstanceId(carPackageAudit.getInstanceId()).singleResult();
        taskService.setVariable(currentTask.getId(), "auditStatus", auditStatus);
        taskService.addComment(currentTask.getId(), carPackageAudit.getInstanceId(), info);
        taskService.complete(currentTask.getId());
        //获取下一个节点
        Task nextTask = taskService.createTaskQuery().processInstanceId(carPackageAudit.getInstanceId()).singleResult();
        if (CarPackageAudit.STATUS_PASS.equals(auditStatus)) {
            if (nextTask != null) {
                //财务审核
                carPackageAudit.setAuditorId(Long.parseLong(nextTask.getAssignee()));
                carPackageAudit.setAuditTime(new Date());
            } else {
                //审核通过
                serviceItemService.changeAuditStatus(carPackageAudit.getServiceItemId(), ServiceItem.AUDITSTATUS_APPROVED);
                carPackageAudit.setStatus(CarPackageAudit.STATUS_PASS);
                carPackageAuditMapper.changeAudits(id, CarPackageAudit.AUDITORS_ALREADY);
            }
        } else {
            //审核拒绝
            serviceItemService.changeAuditStatus(carPackageAudit.getServiceItemId(), ServiceItem.AUDITSTATUS_INIT);
            carPackageAudit.setStatus(CarPackageAudit.STATUS_REJECT);
        }
        carPackageAuditMapper.update(carPackageAudit);
    }
}
